package com.resourcestore.action;
import java.io.UnsupportedEncodingException;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.util.ValueStack;
import com.resourcestore.utils.CommonUtil;
import com.resourcestore.utils.tag.Pagination;
import com.resourcestore.action.BaseAction;
import com.resourcestore.dao.CourseFileSrcCatePublicDao;
import com.resourcestore.dao.CourseDao;
import com.resourcestore.model.CourseFileSrcCatePublic;
import com.resourcestore.model.Course;
import com.resourcestore.model.MajorFileSrcCate;

public class CourseFileSrcCatePublicAction extends BaseAction {
	
	private CourseFileSrcCatePublic courseFileSrcCatePublic;
	private List<CourseFileSrcCatePublic> listCourseFileSrcCatePublic;
	private CourseFileSrcCatePublicDao courseFileSrcCatePublicDao = new CourseFileSrcCatePublicDao();
	private Pagination pagination = new Pagination();
	private List<Course> listCourse;
	private CourseDao courseDao = new CourseDao();
	
	/**
	 * 查询返回List
	 * @throws ParseException 
	 * 
	 */
	public String listCourseFileSrcCatePublic(){
		
		courseFileSrcCatePublic = new CourseFileSrcCatePublic();
		courseFileSrcCatePublic.setIsPreCourseCate(-1);
		
		listCourseFileSrcCatePublic = courseFileSrcCatePublicDao.selectCourseFileSrcCatePublic(courseFileSrcCatePublic);
		
		// 将bigint数字处理为时间格式
		for (int i = 0; i < listCourseFileSrcCatePublic.size(); i++) {
			try {
				listCourseFileSrcCatePublic.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listCourseFileSrcCatePublic
								.get(i).getGmtCreated()));
				listCourseFileSrcCatePublic.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listCourseFileSrcCatePublic
								.get(i).getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//设置预置分类字符串
			if (listCourseFileSrcCatePublic.get(i).getIsPreCourseCate() == 0) {
				listCourseFileSrcCatePublic.get(i).setIsPreCourseCateString("否");
			} else if (listCourseFileSrcCatePublic.get(i).getIsPreCourseCate() == 1) {
				listCourseFileSrcCatePublic.get(i).setIsPreCourseCateString("是");
			}
			if(listCourseFileSrcCatePublic.get(i).getCourseFileCateParentId()==-1){
				listCourseFileSrcCatePublic.get(i).setCourseFileCateParentName("根分类");
			}
		}
		
		return "default";
	}
	
	
	/**
	 * 根据条件搜索，查询返回List带分页
	 * @throws ParseException 
	 * 
	 */
	public String searchListCourseFileSrcCatePublic(){
		
		//对内容进行urldecoder解码
		try {
			courseFileSrcCatePublic.setCourseFileCateName(new String(courseFileSrcCatePublic.getCourseFileCateName().getBytes("iso-8859-1"),"UTF-8"));
			courseFileSrcCatePublic.setCourseName(new String(courseFileSrcCatePublic.getCourseName().getBytes("iso-8859-1"),"UTF-8"));
			courseFileSrcCatePublic.setAuthor(new String(courseFileSrcCatePublic.getAuthor().getBytes("iso-8859-1"),"UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//CourseFileCateParentId为0等同于无值替换为-2
		if(courseFileSrcCatePublic.getCourseFileCateParentId()==0){
			courseFileSrcCatePublic.setCourseFileCateParentId(-2);
		}
		pagination.setTotal(courseFileSrcCatePublicDao.queryCourseFileSrcCatePublicNum(courseFileSrcCatePublic));
		listCourseFileSrcCatePublic = courseFileSrcCatePublicDao.selectCourseFileSrcCatePublicPage(courseFileSrcCatePublic,pagination);
		
		// 将bigint数字处理为时间格式
		for (int i = 0; i < listCourseFileSrcCatePublic.size(); i++) {
			try {
				listCourseFileSrcCatePublic.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listCourseFileSrcCatePublic
								.get(i).getGmtCreated()));
				listCourseFileSrcCatePublic.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listCourseFileSrcCatePublic
								.get(i).getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// 设置预置分类字符串
			if (listCourseFileSrcCatePublic.get(i).getIsPreCourseCate() == 0) {
				listCourseFileSrcCatePublic.get(i).setIsPreCourseCateString("否");
			} else if (listCourseFileSrcCatePublic.get(i).getIsPreCourseCate() == 1) {
				listCourseFileSrcCatePublic.get(i).setIsPreCourseCateString("是");
			}
			if (listCourseFileSrcCatePublic.get(i).getCourseFileCateParentId() == -1) {
				listCourseFileSrcCatePublic.get(i).setCourseFileCateParentName("根分类");
			}
		}
		
		return newpath("/CourseFileSrcCatePublic/searchCourseFileSrcCatePublic.jsp");
	}
	
	/**
	 * 多级分类标题文字缩进	
	 */
	public void formatCourseFileSrcCatePublic(List<CourseFileSrcCatePublic> listMFSC){
		
		for (CourseFileSrcCatePublic eachMFSC : listMFSC) {			
			for(int i=0;i<eachMFSC.getDepth();i++){
				eachMFSC.setCourseFileCateName("&nbsp;&nbsp;"+ eachMFSC.getCourseFileCateName());
			}
		}
	}
	
	/**
	 * 多级分类递归函数
	 * 
	 * @param parentChapter
	 * @return
	 */
	public List<CourseFileSrcCatePublic> recursiveListCourseFileSrcCatePublicTree(CourseFileSrcCatePublic parentMFSC) {

		List<CourseFileSrcCatePublic> treeMFSC= new ArrayList<CourseFileSrcCatePublic>();
		List<CourseFileSrcCatePublic> firstStepListMFSC = new ArrayList<CourseFileSrcCatePublic>();// 一级目录
		firstStepListMFSC = courseFileSrcCatePublicDao.selectCourseFileSrcCatePublicByParentId(parentMFSC);
		if (null != firstStepListMFSC && firstStepListMFSC.size() > 0) {
			for (int i = 0; i < firstStepListMFSC.size(); i++) {
				treeMFSC.add(firstStepListMFSC.get(i));

				// 递归时，将majorFileCateParentId换为已查询出来的majorFileCateId
				int newParentChapterId = firstStepListMFSC.get(i).getCourseFileCateId();
				parentMFSC.setCourseFileCateParentId(newParentChapterId);

				treeMFSC.addAll(this.recursiveListCourseFileSrcCatePublicTree(parentMFSC));
			}
		}
		//formatCourseFileSrcCatePublic(treeMFSC);
		return treeMFSC;
	}
	
	/**
	 * 递归更新子分类depth,isPreCourseCate,courseId
	 * 
	 * @param parentChapter
	 * @return
	 */
	public void recursiveUpdateSubCourseFileSrcCatePublicDepth(CourseFileSrcCatePublic parentMFSC) {

		List<CourseFileSrcCatePublic> firstStepListMFSC = new ArrayList<CourseFileSrcCatePublic>();// 一级目录
		firstStepListMFSC = courseFileSrcCatePublicDao.selectCourseFileSrcCatePublicByParentId(parentMFSC);
		if (null != firstStepListMFSC && firstStepListMFSC.size() > 0) {
			for (int i = 0; i < firstStepListMFSC.size(); i++) {
			
				firstStepListMFSC.get(i).setDepth(parentMFSC.getDepth()+1);
				firstStepListMFSC.get(i).setIsPreCourseCate(parentMFSC.getIsPreCourseCate());
				firstStepListMFSC.get(i).setCourseId(parentMFSC.getCourseId());

				try {
					firstStepListMFSC.get(i).setGmtModified(CommonUtil.getSysTimeFormatNum());
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				courseFileSrcCatePublicDao.updateCourseFileSrcCatePublic(firstStepListMFSC.get(i));
				
				// 递归时，将majorFileCateParentId换为已查询出来的majorFileCateId
				int newParentChapterId = firstStepListMFSC.get(i).getCourseFileCateId();
				parentMFSC.setCourseFileCateParentId(newParentChapterId);

				this.recursiveUpdateSubCourseFileSrcCatePublicDepth(parentMFSC);
			}
		}

	}
	
	/**
	 * 预插入数据
	  */		
	public String preInsertCourseFileSrcCatePublic(){
			
		//listCourse = courseDao.selectCourse();
		
		return newpath("/CourseFileSrcCatePublic/insertCourseFileSrcCatePublic.jsp");		
	}
	
	/**
	 * 插入一条数据
	  */		
	public String insertCourseFileSrcCatePublic(){
		
		try {
			courseFileSrcCatePublic.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			courseFileSrcCatePublic.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		courseFileSrcCatePublicDao.insertCourseFileSrcCatePublic(courseFileSrcCatePublic);
		return redirect(this.getUrl());		
	}
	
	/**
	 * 更新之前加载原信息
	 * 
	 */
	public String preUpdateCourseFileSrcCatePublic(){
		
		courseFileSrcCatePublic = courseFileSrcCatePublicDao.selectCourseFileSrcCatePublicById(courseFileSrcCatePublic);
		
		try {
			courseFileSrcCatePublic.setGmtCreatedString(CommonUtil.getSysTimeFormatString(courseFileSrcCatePublic.getGmtCreated()));
			courseFileSrcCatePublic.setGmtModifiedString(CommonUtil.getSysTimeFormatString(courseFileSrcCatePublic.getGmtModified()));
		} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}
		
		return newpath("/CourseFileSrcCatePublic/updateCourseFileSrcCatePublic.jsp");
	}
	/**
	 * 更新信息
	 * 
	 */
	public String updateCourseFileSrcCatePublic(){
		
		try {
			//courseFileSrcCatePublic.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			courseFileSrcCatePublic.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		courseFileSrcCatePublicDao.updateCourseFileSrcCatePublic(courseFileSrcCatePublic);
		
		courseFileSrcCatePublic.setCourseFileCateParentId(courseFileSrcCatePublic.getCourseFileCateId());
		this.recursiveUpdateSubCourseFileSrcCatePublicDepth(courseFileSrcCatePublic);
		return redirect(this.getUrl());
	}

	/**
	 * 删除信息
	 */
	public String deleteCourseFileSrcCatePublic(){
		
		courseFileSrcCatePublicDao.deleteCourseFileSrcCatePublic(courseFileSrcCatePublic);
		courseFileSrcCatePublic.setCourseFileCateParentId(courseFileSrcCatePublic.getCourseFileCateId());
		List<CourseFileSrcCatePublic> listMfsc = new ArrayList<CourseFileSrcCatePublic>();
		listMfsc = this.recursiveListCourseFileSrcCatePublicTree(courseFileSrcCatePublic);
		for(int i=0;i<listMfsc.size();i++){
			courseFileSrcCatePublicDao.deleteCourseFileSrcCatePublic(listMfsc.get(i));
		}
		return redirect(this.getUrl());
	}
	
	/**
	 * 批量删除信息
	 */
	public String deleteMoreCourseFileSrcCatePublic(){
	
		courseFileSrcCatePublicDao.deleteMoreCourseFileSrcCatePublic(courseFileSrcCatePublic);
		return redirect(this.getUrl());
	}

	
	public CourseFileSrcCatePublic getCourseFileSrcCatePublic() {
		return courseFileSrcCatePublic;
	}

	public void setCourseFileSrcCatePublic(CourseFileSrcCatePublic courseFileSrcCatePublic) {
		this.courseFileSrcCatePublic = courseFileSrcCatePublic;
	}

	public List<CourseFileSrcCatePublic> getListCourseFileSrcCatePublic() {
		return listCourseFileSrcCatePublic;
	}

	public void setListCourseFileSrcCatePublic(List<CourseFileSrcCatePublic> listCourseFileSrcCatePublic) {
		this.listCourseFileSrcCatePublic = listCourseFileSrcCatePublic;
	}


	public Pagination getPagination() {
		return pagination;
	}


	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}


	public List<Course> getListCourse() {
		return listCourse;
	}


	public void setListCourse(List<Course> listCourse) {
		this.listCourse = listCourse;
	}
	

}
